package com.tsd.base.service.impl;

import com.tsd.base.dao.BaseUnitMapper;
import com.tsd.base.entity.BaseUnit;
import com.tsd.base.entity.BaseUnitExt;
import com.tsd.base.service.BaseUnitService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseUnit 服务实现层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Service
public class BaseUnitServiceImpl extends BaseServiceImpl implements BaseUnitService {

    @Resource
    private BaseUnitMapper baseUnitMapper;

    @Override
    public BaseUnit getBaseUnitById(Long id) throws Exception {
        return baseUnitMapper.selectByPrimaryKey(id);
    }

    @Override
    public BaseUnitExt queryBySid(String sid) throws Exception {
        return baseUnitMapper.selectBySid(sid);
    }

    @Override
    public void deleteBaseUnit(Long id, SysUser opUser) throws Exception {
        BaseUnit old = baseUnitMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
        baseUnitMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public void batchDeleteUnit(List<String> sids, SysUser opUser) throws Exception {
        for (String sid : sids) {
            BaseUnit old = baseUnitMapper.selectBySid(sid);
            super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
            baseUnitMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public BaseUnit saveBaseUnit(BaseUnit record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getName())) {
            throw new HlpException("名称，不能为空，请输入！");
        } else {
            //判断是否存在相同的编码，如存在不允许添加
            BaseUnit searchUnit = new BaseUnit();
            if (!HlpUtils.isEmpty(record.getId())) {
                searchUnit.setId(record.getId());
            }
            searchUnit.setName(record.getName());
            searchUnit.setDr(0);
            List<BaseUnitExt> baseUnitList = baseUnitMapper.selectByEntity(searchUnit);
            if (!HlpUtils.isEmptyList(baseUnitList)) {
                throw new HlpException("存在相同的名称，请重新输入");
            }
        }
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            baseUnitMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            baseUnitMapper.updateByPrimaryKeySelective(record);
        }
        return record;
    }

    @Override
    public List<BaseUnitExt> findBaseUnits(Map<String, Object> params, PageBean page) throws Exception {
        return baseUnitMapper.selectByPage(params, page);
    }

    @Override
    public List<BaseUnitExt> findBaseUnits(Map<String, Object> params) throws Exception {
        return baseUnitMapper.selectByParams(params);
    }
}
